package com.vmove.childtrain.db.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.dao.RawRowMapper;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.vmove.childtrain.db.DatabaseHelper;
import com.vmove.childtrain.db.entity.Program;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017/12/29.
 *
 * @Description:
 */

public class ProgramDao {
    /**
     * 获取所有程序列表
     * select count(*) buzhou,program.programName，bussinesId from program GROUP BY bussinId;
     */

    /**
     * 根据业务id查找所有步骤，并根据order字段排序
     * select * from program where bussinId=1001 ORDER BY `order`;
     */

    private Dao<Program, Integer> programDaoOpe;
    private DatabaseHelper helper;

    @SuppressWarnings("unchecked")
    public ProgramDao(Context context) {
        try {
            helper = DatabaseHelper.getHelper(context);
            programDaoOpe = helper.getDao(Program.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param id
     * @return
     */
    public Program get(int id) {
        Program program = null;
        try {
            program = programDaoOpe.queryForId(id);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return program;
    }

    public int addProgram(Program program){
        try {
           return programDaoOpe.createOrUpdate(program).getNumLinesChanged();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int deleteProgramById(int id){
        try {
            return programDaoOpe.deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public void editProgram(Program program){
        try {
            programDaoOpe.createOrUpdate(program);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int delProgramByBusinessId(String id){
        try {
            DeleteBuilder builder = programDaoOpe.deleteBuilder();
            builder.where().eq("programId", id);
            return programDaoOpe.delete(builder.prepare());
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public List<Program> getProgramList() throws SQLException {
        return programDaoOpe.queryForAll();
    }

    public List<Program> getProgramListByBusinessId(String bid) throws SQLException {
        GenericRawResults<Program> results =
                programDaoOpe.queryRaw("select programName,orderKey,lightNum,chaoShi,color,id from tb_program where businessId='"+bid+"' ORDER BY orderKey ASC"
                , new RawRowMapper<Program>() {
                    @Override
                    public Program mapRow(String[] columnNames, String[] resultColumns) throws SQLException {
                        Program simpleProgram = new Program();
                        /*simpleProgram.setProgramName(resultColumns[0]);
                        simpleProgram.setOrderKey(Integer.parseInt(resultColumns[1]));
                        simpleProgram.setLightNum(Integer.parseInt(resultColumns[2]));
                        simpleProgram.setChaoShi(Integer.parseInt(resultColumns[3]));
                        simpleProgram.setColor(Integer.parseInt(resultColumns[4]));
                        simpleProgram.setId(Integer.parseInt(resultColumns[5]));*/
                        return simpleProgram;
                    }
                });
        return results.getResults();
    }

}
